var common = require('../../lib/common.js');
var url = require('../../config/url.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    // 上传链接
    uploadUrl: url.domain + '/Index/Upload/video',
    title: '',
    content: '',
    videoList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ id: options.id });
  },

  onVideoComplete(e) {
    const { fileList } = e.detail
    wx.hideLoading();
  },

  onVideoChange(e) {
    const { file, fileList } = e.detail;
    if (file.status === 'uploading') {
      wx.showLoading({ mask: true, title: '上传中' });
    } else if (file.status === 'done') {
      this.setData({ videoList: fileList });
    }
  },
  onVideoRemove(e) {
    const { file, fileList } = e.detail;
    this.setData({
      videoList: fileList.filter((n) => n.uid !== file.uid),
    })
  },
  onChangeValue(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.detail.value;

    let data = {};
    data[name] = value;
    this.setData(data);
  },
  onRelease() {
    let that = this;
    let params = {};
    let videos = [];
    videos = this.data.videoList.map((n) => {
      let data = JSON.parse(n.res.data);
      return data.data.url;
    });


    if (this.data.title == '') {
      wx.showModal({
        title: '提示',
        content: '标题不能为空',
        success(res) { }
      })
      return false;
    }

    if (videos.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择视频',
        success(res) {
        }
      })
      return false;
    }

    params.circle_photo_type_id = this.data.id;
    params.title = this.data.title;
    params.content = this.data.content;
    params.resource_type = 'video';
    params.resource = videos.join(',');

    wx.showLoading({ mask: true });
    common.post(url.addCirclePhoto, params, (res) => {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        content: '新增成功',
        success(res) {
          wx.navigateBack({
            delta: 1
          });
        }
      })
    });
  }


})